package com.example.navigationdemo.nav.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.example.navigationdemo.R;

public class LottieTabView extends FrameLayout {

    private int mTextNormalColor;
    private int mTextSelectColor;
    private int mTextNormalSize;
    private int mTextSelectSize;
    private String mTabName;
    private Drawable mIconNormal;
    private String mAnimationPath;
    private LottieAnimationView mLottieView;
    private TextView mTabNameView;
    private TextView mMsgView;
    private boolean isSelected;
    private boolean isLottie;
    private String pageUrl;
    private String title;

    public LottieTabView(Context context) {
        this(context, null);
    }

    public LottieTabView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LottieTabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LottieTabView);
        mTextNormalColor = ta.getColor(R.styleable.LottieTabView_text_normal_color, Color.BLACK);
        mTextSelectColor = ta.getColor(R.styleable.LottieTabView_text_selected_color, Color.BLUE);
        mTextNormalSize = (int) ta.getDimension(R.styleable.LottieTabView_text_size, dp2px(5));
        mIconNormal = ta.getDrawable(R.styleable.LottieTabView_icon_normal);
        mAnimationPath = ta.getString(R.styleable.LottieTabView_lottie_path);
        mTabName = ta.getString(R.styleable.LottieTabView_tab_name);
        isSelected = ta.getBoolean(R.styleable.LottieTabView_tab_selected, false);
        ta.recycle();
        initView(context);
    }

    private void initView(Context context) {
        View containView = LayoutInflater.from(context).inflate(R.layout.lottie_tab_view, null, false);
        mLottieView = containView.findViewById(R.id.animation_view);
        mLottieView.setRepeatCount(0);
        mTabNameView = containView.findViewById(R.id.tab_name);
        mTabNameView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextNormalSize);
        mTabNameView.setTextColor(mTextNormalColor);
        mTabNameView.setText(mTabName);
        mMsgView = containView.findViewById(R.id.msg_view);
        this.addView(containView);
        if (isSelected) {
            selected();
        } else {
            unSelected();
        }
    }

    public void setTextNormalColor(int color) {
        mTextNormalColor = color;
    }

    public void setTextSelectColor(int color) {
        mTextSelectColor = color;
    }

    public void setTextNormalSize(int textSize) {
        mTextNormalSize = textSize;
    }

    public void setTextSelectSize(int textSize) {
        mTextSelectSize = textSize;
    }

    public void setPageUrl(String pageUrl) {
        this.pageUrl = pageUrl;
    }

    public void setAnimationPath(String animationPath) {
        mAnimationPath = animationPath;
    }

    public void isLottieView(boolean isLottie) {
        this.isLottie = isLottie;
    }

    public void setTitle(String title) {
        this.title = title;
        if (TextUtils.isEmpty(title)) {
            mTabNameView.setVisibility(View.GONE);
        } else {
            mTabNameView.setText(title);
        }
    }

    public void setIconNormal(Drawable drawable) {
        mIconNormal = drawable;
    }

    /**
     * 选中状态
     */
    public void selected() {
        if (!isLottie) {
            return;
        }
        if (TextUtils.isEmpty(mAnimationPath)) {
            throw new NullPointerException("ainmation path must be not empty");
        } else {
            mLottieView.setAnimation(mAnimationPath);
            mLottieView.playAnimation();
            if (!TextUtils.isEmpty(title)) {
                mTabNameView.setTextColor(mTextSelectColor);
                mTabNameView.setTextSize(mTextSelectSize);
            }
        }
    }

    /**
     * 未选中
     */
    public void unSelected() {
        mLottieView.clearAnimation();
        mLottieView.setImageDrawable(mIconNormal);
        if (!TextUtils.isEmpty(title)) {
            mTabNameView.setTextColor(mTextNormalColor);
            mTabNameView.setTextSize(mTextNormalSize);
        }
    }

    /**
     * 显示红点消息数，默认不显示
     *
     * @param num
     */
    public void msgNum(int num) {
        if (num > 0 && num <= 99) {
            mMsgView.setVisibility(VISIBLE);
            mMsgView.setText(num + "");
        } else if (num > 99) {
            mMsgView.setVisibility(VISIBLE);
            mMsgView.setText("99+");
        } else {
            mMsgView.setVisibility(View.INVISIBLE);
        }
    }

    public void setIconSize(int size) {
        if (mLottieView != null) {
            ViewGroup.LayoutParams layoutParams = mLottieView.getLayoutParams();
            layoutParams.width = dp2px(size);
            layoutParams.height = dp2px(size);
            mLottieView.setLayoutParams(layoutParams);
        }
    }

    private int dp2px(int dpValue) {
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        return (int) (metrics.density * dpValue + 0.5f);
    }

    public String getPageUrl() {
        return pageUrl;
    }
}
